﻿#if UNITY_EDITOR

using Memo.Electricity.Game;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Memo.Electricity.Editor
{
	[CustomEditor(typeof(CMovePlatform))]
	public class MovePlatformEditor : UnityEditor.Editor
	{
		private CMovePlatform mMovePlatform = null;

		private void OnEnable()
		{
			mMovePlatform = target as CMovePlatform;
		}

		public override void OnInspectorGUI()
		{
			base.OnInspectorGUI();

			EditorGUILayout.BeginVertical();
			int deletePos = -1;
			const int size = 64;
			EditorGUILayout.Separator();
			EditorGUILayout.Separator();
			Rect rect = EditorGUILayout.BeginVertical();
			GUI.Box(rect, "节点编辑");
			EditorGUILayout.Separator();
			for (int i = 0; i < mMovePlatform.NodeCount; i++)
			{
				EditorGUI.BeginChangeCheck();
				EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField($"Node {i}", GUILayout.Width(size));
				GUILayout.FlexibleSpace();
				if (i != 0 && GUILayout.Button("Delete", GUILayout.Width(size)))
					deletePos = i;
				EditorGUILayout.EndHorizontal();
				float newWaitTime = EditorGUILayout.FloatField("WaitTime", mMovePlatform.mWaitTimes[i]);
				Vector3 newVec3 = mMovePlatform.mLocalPositions[i];
				if (i != 0)
				{
					newVec3 = EditorGUILayout.Vector3Field("Position", mMovePlatform.mLocalPositions[i]);
					
				}
				EditorGUILayout.Separator();

				if (EditorGUI.EndChangeCheck())
				{
					Undo.RecordObject(target, "Change node property");
					mMovePlatform.mWaitTimes[i] = newWaitTime;
					mMovePlatform.mLocalPositions[i] = newVec3;
				}
			}
			EditorGUILayout.EndVertical();
			
			if (deletePos != -1)
			{
				Undo.RecordObject(target, "Delete Node");
				ArrayUtility.RemoveAt(ref mMovePlatform.mLocalPositions, deletePos);
				ArrayUtility.RemoveAt(ref mMovePlatform.mWaitTimes, deletePos);
			}
			if(GUILayout.Button("Add Node", GUILayout.Width(size)))
			{
				Undo.RecordObject(target, "Add Node");
				ArrayUtility.Add(ref mMovePlatform.mLocalPositions, Vector3.zero);
				ArrayUtility.Add(ref mMovePlatform.mWaitTimes, 0.0f);
			}
			EditorGUILayout.EndVertical();

		}

		private void OnSceneGUI()
		{
			for (int i = 0; i < mMovePlatform.NodeCount; i++)
			{
				Vector3 worldPos = GetWorldPos(i);
				if (i != 0)
				{
					Handles.color = Color.red;
					Vector3 newWorld = Handles.PositionHandle(worldPos, Quaternion.identity);
					if (newWorld != worldPos)
					{
						Undo.RecordObject(target, "Change node property");
						mMovePlatform.mLocalPositions[i] = mMovePlatform.transform.InverseTransformPoint(newWorld);
					}
						
				}

				if (i == 0)
				{
					if (mMovePlatform.PlatformType != CMovePlatform.EMovePlatformType.LOOP)
						continue;
					Handles.DrawDottedLine(GetWorldPos(mMovePlatform.NodeCount - 1), worldPos, 10);
				}
				else
				{
					Handles.DrawDottedLine(GetWorldPos(i - 1), worldPos, 10);
				}
			}

		}

		private Vector3 GetWorldPos(int index)
		{
			if (Application.isPlaying)
			{
				return mMovePlatform.WorldPositions[index];
			}
			else
			{
				return mMovePlatform.transform.TransformPoint(mMovePlatform.mLocalPositions[index]);
			}
		}

	}
}



#endif